package com.kcnet.qk.controller.addfriend;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 添加好友的好友
 */

public class AddFriendFriendCTR extends BasePeopleController {

    private static String TAG = "添加好友的好友";
    private int maxAddFriendCount = 3; //最大添加10人
    private int currentAddFriendCount = 0; //当前任务添加人数
    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());
    private static AddFriendFriendCTR mRongCloudInstance;

    private boolean friendHomeIsBack = false;


    private boolean isJoinFriendsList;
    private boolean isJoinFriendsDetail;
    private boolean isJoinFriendsAndFriendsList;

    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddFriendFriendCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddFriendFriendCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddFriendFriendCTR();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击加好友请求模块");
            // sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_JHYQQ));
            sleepTimedDefault();
            searchClick();
        } else if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.DWZ")) {
            NLogger.d(TAG, "2、点击 全部好友");
            searchTypeClick(FBStringUtil.getString(StringConst.ADDFRIEND_ALL_FRIEND));
            initFbMain = true;
            isJoinFriendsList = true;
        } else if (isJoinFriendsList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            //执行点击好友列表
            NLogger.d(TAG, "3、进入好友列表 开始自动点击好友列表");
            beginAddFriendList();
        } else if (isJoinFriendsDetail && mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView")) {
            NLogger.d(TAG, "4、进入好友详情界面 开始点击好友按钮");

            boolean falg = RootShell.click(360,635);
            if (falg) {
                isJoinFriendsAndFriendsList = true;
                isJoinFriendsDetail = false;
            } else {
                isJoinFriendsList = true;
                isJoinFriendsDetail = false;
                friendHomeGotoBack();
            }
            NLogger.d(TAG, "5、点击好友按钮 状态：" + falg);
        } else if (isJoinFriendsAndFriendsList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            //执行点击好友列表
            NLogger.d(TAG, "6、进入好友的好友列表 开始自动点击 加好友");
            beginAddFriend();
        } else if (friendHomeIsBack && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "7、当前好友的好友添加任务执行完毕 返回到好友列表");
            friendHomeIsBack = false;
            isJoinFriendsList = true;
            friendHomeGotoBack();
        } else if (isJoinFriendsList && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "8、回到好友列表 继续点击好友进行添加");
            beginAddFriendList();
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")) { //弹出对话框自动点击确认
            if (findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEDING))) {
            } else {
                findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEREN));
            }
        }

    }


    public void friendHomeGotoBack() {
        AccessibilityNodeInfo root = getRoot();
        List<AccessibilityNodeInfo> list = root.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.BACK));
        if (!list.isEmpty()) {
            NodeUtil.performClick(list.get(0));
            NLogger.d(TAG, "点击返回 回到好友主页 并且再次回退");
        }
    }

    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendList() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }

                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {
                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        mRecordMap.clear();
                        stopTask();
                        //停止任务
                        break;
                    }
                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(i);
                    Log.d("ND", "nodeInfo size" + nodeInfo.getChildCount());
                    if (nodeInfo.getChildCount() > 2) {
                        if (!mRecordMap.containsKey(nodeInfo.getContentDescription().toString())) {

                            sleepTimedDefault();
                            isJoinFriendsDetail = NodeUtil.performClick(nodeInfo);
                            if (isJoinFriendsDetail) {
                                mRecordMap.put(nodeInfo.getContentDescription().toString(), true);
                                currentAddCount++;
                                isJoinFriendsList = false;
                                break;
                            }

                            NLogger.d(TAG, "点击了好友列表 " + isJoinFriendsDetail);

                            break;
                        }

                    }

                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }

                }


            }
        }
    }


    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriend() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }
                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {
                    if (currentAddFriendCount == maxAddFriendCount) {
                        currentAddFriendCount = 0;
                        friendHomeIsBack = true;
                        isJoinFriendsAndFriendsList = false;
                        friendHomeGotoBack();
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(i);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.ADDFRIEND_JIAWEIHAOYOU));

                    for (AccessibilityNodeInfo accessibilityNodeInfo : list) {
                        currentAddFriendCount++;

                        NLogger.d(TAG, "点击了加为好友" + NodeUtil.performClick(accessibilityNodeInfo));
                        break;
                    }

                }


            }
        }
    }

    public void setParms(int maxAddCount, int minAddTime, int maxAddTime) {
       // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.maxAddFriendCount = maxAddCount;
        this.mRecordMap.clear();
        initFbMain = false;
        isJoinFriendsList = false;
        isJoinFriendsDetail = false;
        friendHomeIsBack = false;
        isJoinFriendsAndFriendsList = false;
    }


}
